//logs.js
//获取应用实例
const app = getApp()

Page({
  data:{
    iconClass: '', //icon class
    title: '', //标题
    desc: '', //描述
    orderNo: '', //订单编号
    amount: '0', //支付金额
    resultType: '', //支付结果 1成功 2失败
    pageType: '', //页面类型 1单次购买 2vip购买
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    var type = options.type
    var orderNo = options.orderNo
    var pageType = options.pageType

    that.findOrder(orderNo) //获取订单信息

    if(type == '1'){ //支付成功
      let payDesc = ""
      var wxUserId = app.globalData.wxUserId
      app.http.get('/wechat/getUserInfo?wxUserId=' + wxUserId).then(res =>{
        if(res.status == '200'){
          let userInfo = res.data
          app.globalData.wxUserType = userInfo.userType
          if(pageType == 1){ //1单次购买
            payDesc = "支付成功，您有一次使用机会。"
          } else {
            payDesc = "支付成功，会员"+userInfo.dueTime+"到期"
          }
          that.setData({
            pageType: pageType,
            resultType: type,
            orderNo: orderNo,
            iconClass: 'weui-icon-success',
            title: '支付成功',
            desc: payDesc,
          })
        }
      })
    } else { //支付失败
      that.setData({
        pageType: pageType,
        resultType: type,
        orderNo: orderNo,
        iconClass: 'weui-icon-warn',
        title: '支付失败',
        desc: '支付失败，请返回重新购买',
      })
    }
  },
  onReady:function(){
   
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
    console.log("-------------pay result onHide-----------------")
  },
  onUnload:function(){
    // 页面关闭
    console.log("-------------pay result onUnload-----------------")
  },
  copyOrderNo: function(){ //复制订单编号
    let that = this
    let no = that.data.orderNo
    wx.setClipboardData({
      data: no,
      success: function(res) {
        wx.showToast({
          title: '订单号已复制！',
        })
      },
      fail: function(res){
        console.log(res)
      }
    })
  },
  reRay: function(){
    wx.navigateBack({})
  },
  index: function () {
    wx.switchTab ({
      url: '/pages/index/index'
    })
  },
  vip: function(){
    wx.reLaunch ({
      url: '/pages/vip/vip'
    })
    // var wxUserId = app.globalData.wxUserId
    // app.http.get('/wechat/getUserType?wxUserId=' + wxUserId).then(res =>{
    //   if(res.status == '200'){
    //     app.globalData.wxUserType = res.data
    //     wx.reLaunch ({
    //       url: '/pages/vip/vip'
    //     })
    //   } else {
    //     wx.reLaunch ({
    //       url: '/pages/vip/vip'
    //     })
    //   }
    // })
  },
  findOrder: function(orderNo){ //获取订单信息
    let that = this
    var data = {
      orderNo: orderNo,
    }
    app.http.post('/wechat/findWebOrder',data).then(res => {
      if(res.status == '200'){
      //  console.log(res.data)
       that.setData({
        amount: res.data.orderAmount
       })
      }
    })
  },
})
